package com.weike.yuanding.configration;

import com.weike.yuanding.configration.RedisKeyExpirationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(
            RedisConnectionFactory connectionFactory,
            RedisKeyExpirationListener expirationListener) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);

        // 关键修改：直接监听具体数据库（如0号库）
        container.addMessageListener(
                expirationListener,
                new PatternTopic("__keyevent@0__:expired") // 替换为你的实际数据库编号
        );

        return container;
    }
}